<?php
/**
 * 前端店铺绑定关系处理
 * @author ome
 * @access public
 * @copyright www.shopex.net.cn 2010
 *
 */
class ome_rpc_response_shop extends ome_rpc_response
{
	/**
	 *  授权给爱管的状态信息
     * 当前端店铺授权给爱管时，ESB中心会调用此方法，以更新绑定店铺的授权状态
	 * 根据shop_id 查出对应的店铺类型 360buy|taobao|paipai... 分别处理session
	 * @param   $session_sdf  授权数据
	 */
	function session_callback($session_sdf){
	
	 $shopObj = &app::get('ome')->model('shop');
	  $node_id = base_rpc_service::$node_id;
	 $shopinfo =  $shopObj->dump(array('node_id'=>$node_id), 'node_type');
	 $node_type=$shopinfo['node_type'];
	 echo $node_id;
	 //根据shop_id 查出对应的店铺类型 360buy|taobao|paipai... 分别处理session
	 switch ($node_type){
	 	case 'taobao':
	 		
	 		$top_parameters = base64_decode($session_sdf['top_parameters']);
	 		parse_str($top_parameters,$out);	 		
	 		$session_sdf = array_merge($session_sdf , $out);
	 		unset($session_sdf['top_parameters']);
	 		var_dump($session_sdf);
	 		//状态，true:正常  false:失败
	 		$status = $session_sdf['agreement'];
	 		//淘宝session
	 		$session = $session_sdf['top_session'];
	 		//昵称：淘宝帐号
	 		$nickname = $session_sdf['visitor_nick'];
	 		break;
	 		
	 	case '360buy':
	 		break;
	 		
	 	case 'paipai':	
	 		break;
	 		
	 }
	 
	 //保存session
	 if($session){
	 	// 更新addon字段
	 	
	 	$addon = array('session'=>$session, 'nickname'=>$nickname);
	 	$data = array('addon'=>$addon);
	 	$filter = array('node_id'=>$node_id);
	 	$shopObj->update($data, $filter);
	 	// 更新KVSTORE登录状态
	 	app::get('ome')->setConf($shop_type.'_session_'.$node_id, $status);
	 }
	 kernel::log("shop_type_session:".time().json_encode($session_sdf));
	 
	 
	}
    /**
     * 更新绑定或解除店铺的状态信息
     * 当前端店铺申请绑定或解除关系时，矩阵中心会调用此方法，以更新绑定店铺的绑定状态
     * @access public
     * @param $result 中心返回的店铺数据
     * @method POST
     */
    function shop_callback($result){
 
        $shop_id = $result['shop_id'];
        $nodes = $_POST;
        $status = $nodes['status'];
        $node_id = $nodes['node_id'];
        $node_type = $nodes['node_type'];
        $filter = array('shop_id'=>$shop_id);
        
        $shopObj = &app::get('ome')->model('shop');
        $shopdetail = $shopObj->dump(array('node_id'=>$node_id), 'node_id');
        
        if ($status=='bind' and !$shopdetail['node_id']){
            if ($node_id){
                $data = array('node_id'=>$node_id,'node_type'=>$node_type,'shop_type'=>$node_type);
                $shopObj->update($data, $filter);
                //给有需要的service发送绑定信息
                foreach(kernel::servicelist('ome_shop_relation') as $object){
                    if(method_exists($object, 'bind')){
                        $object->bind($shop_id);
                    }
                }
                die('1');
            }
        }elseif ($status=='unbind'){
            app::get('ome')->setConf('taobao_session_'.$node_id, 'false');
            $data = array('node_id'=>'');
            $shopObj->update($data, $filter);
            //给有需要的service发送绑定信息
            foreach(kernel::servicelist('ome_shop_relation') as $object){
                if(method_exists($object, 'unbind')){
                    $object->unbind($shop_id);
                }
            }
            die('1');
        }
        die('0');
    }
    
    /**
     * 批量获取订单
     * 此功能属个性定制，如有巧合，纯属意外
     * @param $params 请求查询条件参数
     * @return 订单号数据
     */
    function batch_get_orders(){
        
        $params = $_POST;
        $node_id = $params['to_node_id'];
        $shopObj = &app::get('ome')->model('shop');
        $shop_detail = $shopObj->dump(array('node_id'=>$node_id), 'shop_id');
        $shop_id = $shop_detail['shop_id'];
        
        if (empty($shop_id)) die('');
                
        $from_time = kernel::single('ome_func')->date2time($params['start_time']);
        $end_time = kernel::single('ome_func')->date2time($params['end_time']);
        $page = $params['page'] ? $params['page'] : '1';
        $limit = $params['limit'] ? $params['limit'] : '-1';
        if ($limit == '-1'){
            $lim = '0';
        }else{
            $lim = ($page-1)*$limit;
        }
        $return_order_list = array();
        if ($from_time && $end_time){
            $orderObj = &app::get('ome')->model('orders');
            $filter = array('createtime|between'=>array($from_time,$end_time), 'shop_id'=>$shop_id);
            $order_list = $orderObj->getList('order_bn', $filter, $lim, $limit);
            if ($order_list){
                foreach ($order_list as $val){
                    $return_order_list[] = $val['order_bn'];
                }
            }
        }
        $return_order_list = array(
            'total' => count($return_order_list),
            'tid' => $return_order_list,
        );
        die(json_encode($return_order_list));
    }
    
}